#include "mainwindow.h"

#include "component/window.h"
#include "statusbar/StatusBar.h"
#include "title/TitleBar.h"
#include "router.h"

#include <QLabel>
#include <qapplication.h>
#include <qguiapplication.h>
#include <qnamespace.h>
#include <qpoint.h>
#include <qscreen.h>
#include <qsize.h>
#include <qsplitter.h>
#include <qwindowdefs.h>
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

#include "utils/utils.h"
#include "serial_widget.h"
#include "tcp_widget.h"

MainWindow::MainWindow(QWidget *parent) : Window(parent)
{
    resize(1200, 740);
    setWindowFlags(Qt::FramelessWindowHint);
    Window::setIsWindowMax(false);

    initUI();
}

MainWindow::~MainWindow() {}

void MainWindow::initUI() {
    stackedWidget = new QStackedWidget(this);
    routerManager = new RouterManager(stackedWidget, this);

    initTitleBar();
    initStatusBar();
    initMenuTreeList();
    setStackedWidget();
    initCenterContext();
    initDockWidget();
    loadStyleSheet();
}

void MainWindow::initTitleBar()
{
    setMenuWidget(nullptr);
    titleBar = new TitleBar(this);
    titleBar->raise();
    setMenuWidget(titleBar);

    connect(titleBar, &TitleBar::signalButtonMinClicked, this, &MainWindow::onButtonMinClicked);
    connect(titleBar, &TitleBar::signalButtonRestoreClicked, this, &MainWindow::onButtonRestoreClicked);
    connect(titleBar, &TitleBar::signalButtonMaxClicked, this, &MainWindow::onButtonMaxClicked);
    connect(titleBar, &TitleBar::signalButtonCloseClicked, this, &MainWindow::onButtonCloseClicked);
}

void MainWindow::initStatusBar()
{
    statusBar = new StatusBar(this);
    setStatusBar(statusBar);
}

void MainWindow::initMenuTreeList()
{
    menuTreeList = new MenuTreeList(this);
    // 添加菜单项
    QTreeWidgetItem *network = menuTreeList->addMenuItem("网络助手",svgToIcon(":/resources/icon/network.svg"));
    QTreeWidgetItem *tcpItem = menuTreeList->addMenuItem("TCP助手", svgToIcon(":/resources/icon/TCP.svg"), network);
    QTreeWidgetItem *udpItem = menuTreeList->addMenuItem("UDP助手", svgToIcon(":/resources/icon/UDP.svg"), network);
    tcpItem->setData(0, Qt::UserRole, QVariant::fromValue(Router{"tcp_route", {}}));
    udpItem->setData(0, Qt::UserRole, QVariant::fromValue(Router{"udp_route", {}}));

    QTreeWidgetItem *serialPort = menuTreeList->addMenuItem("串口助手", svgToIcon(QString(":/resources/icon/serial.svg")));
    serialPort->setData(0, Qt::UserRole, QVariant::fromValue(Router{"serial", {}}));

    connect(menuTreeList, &MenuTreeList::itemClicked, this, &MainWindow::onItemClicked);
}

// 注册面板
void MainWindow::setStackedWidget() {
    routerManager->registerRouter(QString("serial"), new SerialWidget(stackedWidget));
    routerManager->registerRouter(QString("tcp_route"), new TcpWidget(stackedWidget));
    routerManager->navigateTo(QString("tcp_route"));
}

void MainWindow::initCenterContext()
{
    splitter = new QSplitter(Qt::Horizontal, this);
   
    // 将控件添加到 Splitter
    splitter->addWidget(menuTreeList);
    splitter->addWidget(stackedWidget);

    // 设置伸缩比例
    splitter->setStretchFactor(0, 0); // 左侧占比
    splitter->setStretchFactor(1, 1); 

    // 设置中央控件
    setCentralWidget(splitter);
}

void MainWindow::loadStyleSheet() {
    QFile file(QString(":/resources/style/mainwindow.qss"));
    file.open(QFile::ReadOnly);
    if (file.isOpen())
    {
        QString styleSheet = this->styleSheet();
        styleSheet += file.readAll();
        this->setStyleSheet(styleSheet);
    }
}

void MainWindow::onButtonMinClicked()
{
    if (Qt::Tool == (windowFlags() & Qt::Tool))
    {
        hide(); // 是否设置了Qt::Tool标志来决定是隐藏窗口还是调用showMinimized()来最小化窗口
    }
    else
    {
        showMinimized();
    }
}

void MainWindow::onButtonRestoreClicked()
{
    QPoint windowPos;
    QSize windowSize;
    titleBar->getRestoreInfo(windowPos, windowSize);
    this->setGeometry(QRect(windowPos, windowSize));
    Window::setIsWindowMax(false);
}

void MainWindow::onButtonMaxClicked()
{
    titleBar->saveRestoreInfo(this->pos(), QSize(this->width(), this->height()));
    // QRect desktopRect = QGuiApplication::primaryScreen()->availableGeometry();
    // QRect FactRect = QRect(desktopRect.x(), desktopRect.y(), desktopRect.width(), desktopRect.height());
    // setGeometry(FactRect);
    QScreen *screen = QGuiApplication::screenAt(this->pos());
    if (screen)
    {
        QRect desktopRect = screen->availableGeometry();
        setGeometry(desktopRect);
        Window::setIsWindowMax(true);
    }
    // showMaximized();
}

void MainWindow::onButtonCloseClicked()
{
    close();
}

// 初始化停靠窗口
void MainWindow::initDockWidget()
{
}

// 菜单点击事件处理
void MainWindow::onItemClicked(const QTreeWidgetItem *item)
{
    QVariant data = item->data(0, Qt::UserRole);
    if (data.canConvert<Router>())
    {
        Router router = data.value<Router>();
        routerManager->navigateTo(router.router_name, router.params);
        qDebug() << "菜单路由处理：" << router.router_name;
        QString itemPath = "| ";
        itemPath += menuTreeList->getItemPath(item);
        titleBar->setHeaderInfor(itemPath);
    }
}